﻿@using Partner.Surround.Web.Startup
@using Partner.Surround.Authorization
@{
    //ViewBag.CurrentPageName = PageNames.Templates;
}

<!--模板列表-->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">
            <div class="layui-form">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">模板名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" placeholder="请输入模板名称" autocomplete="off" class="layui-input" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn" lay-submit lay-filter="form_search">
                            <i class="layui-icon layui-icon-search"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <div class="layui-btn-container">
                @*@if (await PermissionChecker.IsGrantedAsync(PermissionNames.Pages_Template_Delete))
                    {
                        <button class="layui-btn layui-btn-template" data-type="batchdel">删除</button>
                    }
                    @if (await PermissionChecker.IsGrantedAsync(PermissionNames.Pages_Template_Create))
                    {
                        <button class="layui-btn layui-btn-template" data-type="add">添加</button>
                    }*@
            </div>
            <div class="layui-row">
                <div class="layui-col-xs12">
                    <table class="layui-table"
                           lay-data="{height: 'full-180', page:true, id:'templateList'}"
                           lay-filter="templateList">
                        <thead>
                            <tr>
                                <th lay-data="{checkbox:true, fixed: true,width:40}"></th>
                                <th lay-data="{field:'lastModificationTime',templet:'#lastModificationTime'}">最后修改时间</th>
                                @*@if (await PermissionChecker.IsGrantedAsync(PermissionNames.Pages_Template_Create_Update))
                                    {
                                        <th lay-data="{align:'center',toolbar:'#templateBarList'}"></th>
                                    }*@
                            </tr>
                        </thead>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

@await Html.PartialAsync("_LastModificationTime")

<!--列表按钮-->
<script type="text/html" id="templateBarList">
    <div class="layui-table-cell laytable-cell-1-0-9">
        @*@if (await PermissionChecker.IsGrantedAsync(PermissionNames.Pages_Template_Update))
            {
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
            }*@
    </div>
</script>

@section Scripts{
    <script type="text/javascript">
        layui.config({
            base: "/lib/layui-extend-modules/"
        }).use(['jquery', 'layer', 'table', 'form'], function () {
            var $ = layui.$,
                table = layui.table,
                form = layui.form,
                layer = layui.layer;

            //监听搜索
            form.on('submit(form_search)', function (data) {
                var field = data.field;

                //执行重载
                loadTemplateList(field);
            });

            //加载表格数据
            var config = {};  //table的参数，如搜索key
            var loadTemplateList = function (options) {
                if (options != undefined) {
                    $.extend(config, options);
                }
                table.reload('templateList', {
                    url: "@Url.Action("GetTemplateList", "Templates")",
                    where: config,
                    parseData: function (res) {
                        return {
                            "code": res.result.code,
                            "msg": res.result.msg,
                            "count": res.result.count,
                            "data": res.result.data
                        };
                    },
                    response: {
                        statusCode: 200
                    }
                });
            };
            loadTemplateList();

            //监听工具条
            table.on('tool(templateList)', function (obj) {
                var data = obj.data;
                switch (obj.event) {
                    case "edit":
                        var templateData = {
                            title: '编辑模板信息',
                            content: "@Url.Action("CreateOrUpdateTemplate", "Templates")" + abp.utils.formatString("?templateId={0}", data.id)
                        };
                        createOrUpdate(templateData);
                        break;
                }
            });

            //创建或更新
            function createOrUpdate(templateData) {
                parent.layer.open({
                    type: 2,
                    title: templateData.title,
                    content: templateData.content,
                    area: ["700px", "520px"],
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        parent.window['layui-layer-iframe' + index].layui.submitForm();
                    }
                });
            }

            //创建或更新完毕回调
            layui.callback = function (data) {
                parent.layer.close(data.index); //关闭弹层
                abp.notify.success(data.msg);
                loadTemplateList();
            }

            //删除模板
            function deleteTemplate(ids) {
                parent.layer.confirm("确认删除?", function (index) {
                    abp.ajax({
                        url: "@Url.Action("DeleteTemplate", "Templates")",
                        data: JSON.stringify(ids),
                        abpHandleError: false
                    }).done(function (data) {
                        if (data.code == 200) {
                            abp.notify.success(data.msg);
                            loadTemplateList();
                        }
                    }).fail(function (jqXHR) {
                        parent.layer.msg(jqXHR.message, { icon: 5 });
                    }).always(function () {
                        parent.layer.close(index);
                    });
                });
            }

            //监听事件处理
            var active = {
                add: function () {
                    var templateData = {
                        title: '添加模板',
                        content:"@Url.Action("CreateOrUpdateTemplate", "Templates")"
                    };
                    createOrUpdate(templateData);
                },
                batchdel: function () {
                    var checkStatus = table.checkStatus('templateList'),
                        checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }

                    var ids = checkData.map(function (d) { return { "id": d.id }; });
                    deleteTemplate(ids);
                }
            };

            //点击事件转发器
            $('.layui-btn.layui-btn-template').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });
    </script>
}


